package redis

import (
	"bluebell/settings"
	"fmt"
	"github.com/go-redis/redis"
	"strconv"
	"strings"
)

var rdb *redis.Client // 申明一个全局的redis变量

func Init(cfg *settings.RedisConfig) (err error) {
	s1 := cfg.Host
	s2 := ":"
	s3 := strconv.Itoa(cfg.Port)
	var build strings.Builder
	build.WriteString(s1)
	build.WriteString(s2)
	build.WriteString(s3)
	addr := build.String()
	// addr := "localhost:6379"
	rdb = redis.NewClient(&redis.Options{
		Addr:     addr, // redis默认端口:6379
		Password: "",
		DB:       0,
		PoolSize: cfg.PoolSize,
	})

	_, err = rdb.Ping().Result()
	return
}

func Close() {
	err := rdb.Close()
	if err != nil {
		fmt.Println("close mysql connect failed")
	}
}
